
package com.admissions.tracnghiem;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.admissions.AdmissionsActivity;
import com.admissions.BaseActivity;
import com.admissions.CallBack;
import com.admissions.R;
import com.admissions.TabGroupActivity;
import com.admissions.adapter.SectionsAdapter;
import com.admissions.model.DeThi;
import com.admissions.model.MonThi;
import com.admissions.network.DataHelper;
import com.admissions.network.HttpHelper;
import com.admissions.network.URL;
import com.admissions.utils.JSONHelper;

public class SectionActivity extends BaseActivity implements CallBack, OnItemClickListener {
    private static final int ITEMS_PER_PAGE_MAX = 9;

    private static final int GRID_NUMBER_COLUMNS = 3;

    private ViewPager mSections;

    private MonThi mMonthi;

    private Resources mRes;

    private ArrayList<View> pages = new ArrayList<View>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.section_screen);
        mRes = getResources();
        mSections = (ViewPager) findViewById(R.id.view_pager);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Bundle bS = intent.getExtras();
        mMonthi = (MonThi) bS.getSerializable(MonThi.MONTHI);

        // Setup Header
        String headerContent = mMonthi.name;
        String headerComment = mRes.getString(R.string.mon_thi_caption);
        Drawable icon = null;

        TabGroupActivity parent = (TabGroupActivity) getParent();
        AdmissionsActivity grandParent = (AdmissionsActivity) parent.getParent();
        grandParent.setupHeader(icon, headerContent, headerComment);

        getData();
    }

    public void bindData() {
        SectionsAdapter adapter = new SectionsAdapter(this, mMonthi.dethis);
        adapter.setIcon(mMonthi.icon);

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout page = (LinearLayout) inflater.inflate(R.layout.grid_sections, null, false);
        GridView grid = (GridView) page.findViewById(R.id.grid_section);
        grid.setOnItemClickListener(this);
        grid.setAdapter(adapter);

        pages.add(page);

        SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(pages);
        mSections.setAdapter(pagerAdapter);
    }

    private class SectionsPagerAdapter extends PagerAdapter {
        private ArrayList<View> pages;

        public SectionsPagerAdapter(ArrayList<View> pages) {
            this.pages = pages;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public int getCount() {
            return pages.size();
        }

        @Override
        public Object instantiateItem(View container, int position) {
            View v = pages.get(position);
            ((ViewPager) container).addView(v);
            return v;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void finishUpdate(View arg0) {

        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        DeThi dethi = (DeThi) parent.getItemAtPosition(position);
        Intent intent = new Intent(getParent(), AllThreadsActivity.class);

        Bundle bS = new Bundle();
        bS.putSerializable(DeThi.DETHI, dethi);
        intent.putExtras(bS);

        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.startChildActivity(AllThreadsActivity.class.getName(), intent);
    }

    private void getData() {
        DataHelper task = new DataHelper(this);
        task.execute(new Void[] {});
    }

    @Override
    public void start() {
        showLoadingDialog();
    }

    @Override
    public void doWorks() {
        // Get json String
        MonThi monthi = mMonthi;
        String url = URL.URL_DETHI + monthi.id;
        String jsonString = HttpHelper.getJsonData(url);

        // Parse json to list object
        mMonthi.dethis = JSONHelper.getAllSections(this, jsonString);
    }

    @Override
    public void onFinish() {
        dismissLoadingDialog();

        //Bind data to UI
        bindData();
    }
}
